/*
*    UNIVERSIDADE DE SÃO PAULO.
*    Author: Marco Aurélio Gerosa (gerosa@ime.usp.br)
*    This project was/is sponsored by RNP and FAPESP.
*
*    This file is part of Groupware Workbench (http://www.groupwareworkbench.org.br).
*
*    Groupware Workbench is free software: you can redistribute it and/or modify
*    it under the terms of the GNU Lesser General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    Groupware Workbench is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public License
*    along with Swift.  If not, see <http://www.gnu.org/licenses/>.
*/
package br.org.groupwareworkbench.eureka.knowledge;

import java.util.ArrayList; 
import java.util.List;

import br.com.caelum.vraptor.Delete;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.ioc.RequestScoped;
import br.com.caelum.vraptor.view.Results;
import br.org.groupwareworkbench.collablet.coop.recommend.RecommendMgrInstance;
import br.org.groupwareworkbench.core.framework.Collablet;
import br.org.groupwareworkbench.core.framework.WidgetInfo;

@RequestScoped
@Resource
public class KnowledgeMgrController {

    private final Result result;
    private final WidgetInfo info;

    public KnowledgeMgrController(Result result, WidgetInfo info) {
        this.result = result;
        this.info = info;
    }

    @Get
    @Path(value = "/groupware-workbench/knowledge/{knowledgeMgr}/list")
    public List<Knowledge> list(KnowledgeMgrInstance knowledgeMgr) {
        addIncludes(knowledgeMgr);
        List<Knowledge> list = new ArrayList<Knowledge>();
        for (Knowledge knowledge : knowledgeMgr.list()) {
            if (knowledge.getTitle().length() == 0 ){
                int sizeCause = knowledge.getCause().length();
                if (sizeCause > 50)
                    sizeCause = 50;
                knowledge.setTitle(knowledge.getCause().substring(0, sizeCause)+" ...");
            }else 
                knowledge.setTitle(knowledge.getEscapedTitle());
            list.add(knowledge);
        }
        return list;
    }

    @Get
    @Path(value = "/groupware-workbench/knowledge/{id}/show")
    public void show(final long id) {
        retrieve(id);
    }

    @Get
    @Path(value="/groupware-workbench/knowledge/{knowledgeMgr}/create")
    public void create(KnowledgeMgrInstance knowledgeMgr) {
        Knowledge knowledge = new Knowledge();
        addIncludes(knowledgeMgr);
        result.include("knowledge", knowledge);
        result.use(Results.representation()).from(knowledge).serialize();
    }

    @Get
    @Path(value = "/groupware-workbench/knowledge/{id}")
    public void retrieve(long id) {
        Knowledge knowledge = Knowledge.findById(id);
        if (knowledge == null) {
            result.notFound();
            return;
        }

        KnowledgeMgrInstance knowledgeMgr = (KnowledgeMgrInstance) knowledge.getCollablet().getBusinessObject();
        addIncludes(knowledgeMgr);
        result.include("knowledge", knowledge);
        result.use(Results.representation()).from(knowledge).serialize();
    }

    @Post
    @Path(value = "/groupware-workbench/knowledge/{knowledgeMgr}")
    public void save(final KnowledgeMgrInstance knowledgeMgr, final Knowledge knowledge) {
        knowledgeMgr.save(knowledge);
        knowledgeMgr.getCollablet().processWidgets(info, knowledge);

        RecommendMgrInstance recommendMgr =
            (RecommendMgrInstance) knowledgeMgr.getCollablet().getDependency("recommendMgr").getBusinessObject();

        recommendMgr.generateRelated(knowledgeMgr.list(), knowledge);

        addIncludes(knowledgeMgr);
        result.use(Results.logic()).redirectTo(KnowledgeMgrController.class).list(knowledgeMgr);
    }
    
   
    @Post
    @Path(value = "/groupware-workbench/knowledge/{id}/updateRelated")
    public void updateRelated(long id) {
        Knowledge knowledge = Knowledge.findById(id);
        if (knowledge == null) {
            result.notFound();
            return;
        }

        Collablet collablet = knowledge.getCollablet();
        KnowledgeMgrInstance knowledgeMgr = (KnowledgeMgrInstance) collablet.getBusinessObject();
        collablet.processWidgets(info, knowledge);

        /*RecommendMgrInstance recommendMgr =
                (RecommendMgrInstance) collablet.getDependency("recommendMgr").getBusinessObject();

        recommendMgr.generateRelated(knowledgeMgr.list(), knowledge);*/

        result.use(Results.logic()).redirectTo(KnowledgeMgrController.class).list(knowledgeMgr);
    }

    @Delete
    @Path(value = "/groupware-workbench/knowledge/{id}")
    public void remove(long id) {
        Knowledge knowledge = Knowledge.findById(id);
        if (knowledge == null) {
            result.notFound();
            return;
        }

        KnowledgeMgrInstance knowledgeMgr = (KnowledgeMgrInstance) knowledge.getCollablet().getBusinessObject();
        knowledge.delete();
        result.use(Results.logic()).redirectTo(KnowledgeMgrController.class).list(knowledgeMgr);
    }

    @Get
    @Path(value = "/groupware-workbench/knowledge/{knowledgeMgr}/copyKnowledgeFromBD")
    public void copyFromBD(KnowledgeMgrInstance knowledgeMgr) {
        knowledgeMgr.copyFromBD();
        result.use(Results.logic()).redirectTo(KnowledgeMgrController.class).list(knowledgeMgr);
    }

    private void addIncludes(KnowledgeMgrInstance knowledgeMgr) {
        result.include("knowledgeMgr", knowledgeMgr);
        knowledgeMgr.getCollablet().includeDependencies(result);
    }
}